//
//  ZRHomeTableViewCell.swift
//  Sina
//
//  Created by 张锐 on 16/3/1.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit
import SnapKit
class ZRHomeTableViewCell: UITableViewCell {
    
    var constraint : Constraint?

    var statusViewModel : ZRHomeCellViewModel?{
    
        didSet{
        
            orginalView.statusViewModel = statusViewModel
            
            bottomView.statusViewModel = statusViewModel
            //清空约束
            constraint?.uninstall()
            //判断 是否是转发微博 更新约束 设置转发视图是否可见，如果可见并赋值
            if statusViewModel?.statusModel?.retweeted_status == nil{
            
                retweetView.hidden = true
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.constraint =  make.top.equalTo(orginalView.snp_bottom).offset(10).constraint
                    
                })
            
            }else{
            
                retweetView.hidden = false
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                   self.constraint = make.top.equalTo(retweetView.snp_bottom).offset(10).constraint
                    
                    
                })
                
                retweetView.statusViewModel = statusViewModel
            
            
            }
            
            
        
        }
    
    
    }
    //重写构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
       
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //更新UI设置约束
    func setupUI(){
    
    
        contentView.addSubview(orginalView)
        
        contentView.addSubview(bottomView)
        
        contentView.addSubview(retweetView)
        
        
        //设置约束
        
        orginalView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.contentView.snp_top)
            
            make.left.equalTo(self.contentView.snp_left)
            
            make.right.equalTo(self.contentView.snp_right)

        }
        bottomView.snp_makeConstraints { (make) -> Void in
            
        self.constraint = make.top.equalTo(retweetView.snp_bottom).offset(10).constraint
            
            make.left.equalTo(self.contentView.snp_left)
            
            make.right.equalTo(self.contentView.snp_right)
            
            make.bottom.equalTo(self.contentView.snp_bottom)
            
            make.height.equalTo(30)
        }
    
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(orginalView.snp_bottom).offset(10)
            
            make.left.equalTo(self.contentView.snp_left)
            
            make.right.equalTo(self.contentView.snp_right)
            
//            make.bottom.equalTo(bottomView.snp_top).offset(10)
            
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            
            make.left.equalTo(self.snp_left)
            
            make.right.equalTo(self.snp_right)
            
            make.bottom.equalTo(self.bottomView.snp_bottom)
            
        }
    
    }
    
    //懒加载
    
    lazy var orginalView : ZRCellOrginal = ZRCellOrginal()
    
    lazy var bottomView : ZRCellBottom = ZRCellBottom()
    
    lazy var retweetView : ZRCellRetweet = ZRCellRetweet()

}
